package compiler;

import edu.towson.cosc.cosc445.interfaces.LexicalAnalyzerInterface;
import java.util.*;
import java.io.*;

public class LexicalAnalyzer implements LexicalAnalyzerInterface{
	
	public String[] terminals = {"#HAI", "#KTHXBYE", "#OBTW", "#TLDR", "#MAEK HEAD", "#OIC", "#GIMMEH TITLE", "#MKAY", "#MAEK PARAGRAF", "#GIMMEH BOLD", 
								"#GIMMEH ITALICS", "#MAEK LIST", "#GIMMEH ITEM", "#GIMMEH NEWLINE", "#GIMMEH SOUNDZ", "#GIMMEH VIDZ", "#I HAS A", "#ITZ", "#VISIBLE"};
	
	/** The next lexeme to be examined**/ 
	public String nextToken = "";
	
	/** The scanner for the input file **/
	public Scanner fileIn;
	
	/** The next character. */
    public String nextCharacter = "";
    
    /** The current position **/
    int currentPosition = 0;
    
    /**
     * parameterless constructor
     * @throws FileNotFoundException
     */
    
    public LexicalAnalyzer(){
    	try{
    		throw new FileNotFoundException();
    	}
    	catch(FileNotFoundException ex){
    		System.out.println("No file was specified. Try specifying a file name and compiling again.");
    	}
    }
    
    public LexicalAnalyzer(File f){
    	try{
    		fileIn = new Scanner(f);
    	}
    	catch(FileNotFoundException ex){
    		System.out.println("The file specified was not found. Try adjusting the file name or path and compiling again.");
    	}
    	
    	//delimiter for scanner so the lexical analyzer examines each character
    	fileIn.useDelimiter("");
    }

	/**
	 * This is the public method to be called when the Syntax Analyzer needs a new
	 * token to be parsed.
	 */
	public void getNextToken() {
		//reset nextToken and get the first character of the new token
		nextToken = "";
		getCharacter();
		//if token starts with #, it is a tag and will be partitioned including spaces
		if(nextCharacter.equals("#")){
			addCharacter();
			//grabs all characters in tag from file. stops when it finds a valid tag or when it hits 15 characters,
			//which is the length of the longest valid tag
			while((!lookupToken()) && (nextToken.length() < 15)){
				getCharacter();
				addCharacter();
			}
			//eliminate space after tag
			do{
				getCharacter();
			} while(isSpace(nextCharacter));
		}
		//otherwise it is not a tag and must be plain text
		else{
			//parses plain text word by word, eliminating spaces
			while(!isSpace(nextCharacter)){
				addCharacter();
				getCharacter();
			}
		}
		//increment position in the file by token #
		currentPosition++;
	}

	/**
	 * gets the next character from the input and places it in the nextCharacter class variable.
	 *
	 * @return the character
	 */
	public void getCharacter() {
		nextCharacter = fileIn.next();
	}

	/**
     * adds the current character to the nextToken.
     */
	public void addCharacter() {
		nextToken += nextCharacter;
	}

	/**
	 * gets the next character from the input and places it in the nextCharacter class variable.
	 *
	 * @param c the current character
	 * @return true, if is space; otherwise false
	 */
	public boolean isSpace(String c) {
		if(c.equals(" ")) return true;
		else return false;
	}
	
	/**
	 * This method checks to see if the current, possible token is legal in the
	 * defined grammar.
	 *
	 * @return true, if it is a legal token, otherwise false
	 */
	public boolean lookupToken() {
		for(String x : terminals){
			if(nextToken.equals(x)) return true;
		}
		return false;
	}
}